package br.com.android.productivecenter;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import br.com.android.R;
import br.com.android.modelo.Calendario;
import br.com.android.modelo.CalendarioAdapter;
import br.com.android.modelo.DocumentAdapter;
import br.com.android.modelo.Documento;
import br.com.android.persistencia.manipularDados;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;

public class GoogleCalendarActivity extends Activity {
	
	public manipularDados persistencia;
	private ListView listagemComp;
	public String userName;
	public String Ndescricao;
	public String Ntitulo;
	public String titulo;
	public String Nhora;
	public String Ndata;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        if(getIntent().getStringExtra("userName") != null)
        	userName = getIntent().getStringExtra("userName");
        
       if(getIntent().getExtras().getParcelable("nelson") != null)
        	persistencia = getIntent().getExtras().getParcelable("nelson"); 
       
       try {
			persistencia.carregaCalendario(persistencia.getListaCalendar(), userName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(getIntent().getStringExtra("Ndescricao") != null &&
		   getIntent().getStringExtra("Ntitulo") != null &&
		   getIntent().getStringExtra("Antigotitulo") != null &&
		   getIntent().getStringExtra("Ndata") != null &&
		   getIntent().getStringExtra("Nhora") != null){
			
			Ndescricao = getIntent().getStringExtra("Ndescricao");
			Ntitulo = getIntent().getStringExtra("Ntitulo");
			titulo = getIntent().getStringExtra("Antigotitulo");
			Ndata = getIntent().getStringExtra("Ndata");
			Nhora = getIntent().getStringExtra("Nhora");
			
			Calendario compP = persistencia.getComp(persistencia.getListaCalendar(), titulo);
        	
        	if(compP != null){
        		
        		compP.setDescricao(Ndescricao);
        		compP.setTitulo(Ntitulo);
        		compP.setData(Ndata);
        		compP.setHora(Nhora);
        	}
        	
        	File file = this.getFilesDir();
        	String arq = "/" + userName + "comp.txt";
    		File arquivo = new File(file + arq);
    		
    		arquivo.delete();
    		
        	try {
    			persistencia.guardaCalendar(persistencia.getListaCalendar(),userName);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
		}
        CarregarTelaGoogleCalendar();
    }
	
	 public void CarregarTelaGoogleCalendar(){
	    	
	    	setContentView(R.layout.googlecalendar);
	    	
	    	List <Calendario> listaComp = new ArrayList<Calendario>();
	    	
			for (int i = 0; i < persistencia.getListaCalendar().size(); i++) {
				listaComp.add(persistencia.getListaCalendar().get(i));
				
			}
	    	
			listagemComp = (ListView) findViewById(R.id.listaComp);
			listagemComp.setAdapter(new CalendarioAdapter(GoogleCalendarActivity.this, listaComp, persistencia, userName));
			listagemComp.setItemsCanFocus(true);
			
	    	Button buttonDocs = (Button)
	    	findViewById(R.id.docsB);
	    	buttonDocs.setOnClickListener(new View.OnClickListener(){
	    	
	    		public void onClick(View arg0) {
	    				Intent i = new Intent(GoogleCalendarActivity.this,GoogleDocumentsActivity.class);
	    				i.putExtra("nelson", persistencia);
	    				i.putExtra("userName", userName);
	    				startActivity(i);
	    		}
	    		
	    	});
	    	
	    	Button deslogar = (Button) findViewById(R.id.deslogarB);
	    	deslogar.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent i = new Intent(GoogleCalendarActivity.this,ProductiveCenterActivity.class);
					i.putExtra("nelson", persistencia);
					startActivity(i);
					
				}
			});
	    	
	    	Button novo = (Button) findViewById(R.id.novoComp);
	    	novo.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent i = new Intent(GoogleCalendarActivity.this,NovoCompromissoActivity.class);
					i.putExtra("nelson", persistencia);
					i.putExtra("userName", userName);
					startActivity(i);
					
				}
			});
	    }
}
